//
// File:   _main.cc
// Author: ben
//
// Created on December 4, 2008, 3:08 PM
//

#include <stdlib.h>
#include <vector>

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <math.h>
//#include <octave-2.9.9/octave/oct.h>
//
//
//
using namespace std;

int main(int argc, char** argv) {
	vector<double> w;
	double temp;
	ostringstream fname;
	ifstream fin;
	vector<double> matrix;
	int j,i,k,n;
	
	
	
	
	ifstream fw("weight.dat");
	if(fw.fail()){
		return(-1);
	}
	while(fw.eof()==false){
		fw >> temp;
		w.push_back(temp);
	}
	fw.close();
	
	for(i=1;i<=w.size();i++){
		fname.str("");
		fname << "results" << i << ".dat";
		cout << "Opening " << fname.str().data() << endl;
		fin.open(fname.str().data());
		j=0;
		if(fin.fail()){
			cout << "NOT FOUND: " << fname.str().data() << endl;
			return(-1);
		}
		
		while(fin.eof()==false){
			fin >> temp;
			if(i==1){
				matrix.push_back(temp*w[i-1]);
			}
			else{
				matrix[j]+=temp*w[i-1];
			}
			j++;
		}
		fin.close();
	}
	n	=(int)floor(sqrt(j));
	cout << n << endl;
	ofstream foutlinks("links_graph.dat");
	cout << "output" << endl;
	
	for(i=0;i<n;i++){
		for(k=0;k<n;k++){
			if(matrix[i*n+k]>0){
				foutlinks << i+1 << "\t" << k+1 << "\t" << matrix[i*n+k] << endl;
			}
		}
		
	}
	foutlinks.close();
	return (EXIT_SUCCESS);
}

